package com.dolphin.artifact.controller;

import com.dolphin.artifact.common.utils.EhcacheUtil;
import com.dolphin.artifact.service.UserInfoService;
import com.dolphin.artifact.common.model.ResultModel;
import com.dolphin.artifact.dao.entity.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by hhwf on 2017/10/15.
 */
@Api(value="用户相关的接口")
@RestController
public class UserInfoController {

    Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;



    @GetMapping(value = "/userinfo")
    @ApiOperation(notes="用户查询",value="根据id查询",httpMethod="GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",paramType="query",dataType="long")
    })
    public Object getUserInfo(Long id){
        EhcacheUtil.getInstance().put("11111",11111);
        logger.info("==========>时间 {}",new Date());
        System.out.println(EhcacheUtil.getInstance().get("11111"));

        ResultModel rm = new ResultModel();
        rm = userInfoService.getUserInfoById(id);
        return rm;
    }


    @PostMapping("/add")
    @ApiOperation(notes="添加用户",value="添加一个用户",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="UserName",paramType="query",dataType="string"),
            @ApiImplicitParam(name="Password",paramType="query",dataType="string")
    })
    public Object addUser(UserInfo userInfo){
        ResultModel rm = new ResultModel();
        Date now = new Date();
        userInfo.setCreateTime(now);
        userInfo.setUpdateTime(now);
        userInfoService.addUser(userInfo);
        return rm;
    }

    @PostMapping("/login")
    @ApiOperation(notes="登录",value="登录",httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",paramType="query",dataType="string"),
            @ApiImplicitParam(name="password",paramType="query",dataType="string"),
    })
    public Object login(HttpServletRequest request){
        return userInfoService.login(request.getParameter("userName"),request.getParameter("password"));
    }

}
